<?php

class Curl {

	protected $curl;

	function __construct($url, $vars = null) {
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_COOKIEJAR, 'curl_cookie.txt');
		curl_setopt($ch, CURLOPT_COOKIEFILE, 'curl_cookie.txt');
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);		
		$this->curl = $ch;
	}
	
	static function instance($url) {
		return new self($url);
	}
	
	function set( $param, $val ) {
		curl_setopt( $this->curl, $param, $val );
		return $this;
	}
	
	function get($asArray = true) {
		$data = curl_exec( $this->curl );
		if( $data === false ) throw new Exception( curl_error($this->curl) );
		if( $asArray ) {
			parse_str( $data, $dataArray );
			return $dataArray;
		}
		else return $data;
	}
		
	function post($vars, $asArray = true) {
		$this->set( CURLOPT_POST, 1);
		$this->set( CURLOPT_POSTFIELDS, http_build_query($vars) );
		$data = curl_exec( $this->curl );
		if( $data === false ) throw new Exception( curl_error($this->curl) );
		if( $asArray ) {
			parse_str( $data, $dataArray );
			return $dataArray;
		}
		else return $data;
	
	}
		
	function close() {
		curl_close($this->curl);
	}

}